<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图 -->
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="addDialogVisible = true">添加角色</el-button>
                </el-col>
            </el-row>

            <!-- 角色列表区域 -->
            <el-table :data="rolesList" border stripe>
                <!-- 展开列 -->
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <el-tag type="info" v-if="scope.row.children.length === 0">该角色暂无任何权限</el-tag>
                        <el-row :class="['bdbottom',i1 === 0 ? 'bdtop' : '','vcenter']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
                            <!-- 一级权限 -->
                            <el-col :span="5">
                                <el-tag closable @close="removeRightById(scope.row,item1.id)">{{item1.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <!-- 二级和三级权限 -->
                            <el-col :span="19">
                                <el-row :class="[i2 === 0 ? '' : 'bdtop','vcenter']" v-for="(item2,i2) in item1.children" :key="item2.id">
                                    <!-- 二级权限 -->
                                    <el-col :span='6'>
                                        <el-tag type="success" closable @close="removeRightById(scope.row,item2.id)">{{item2.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <!-- 三级权限 -->
                                    <el-col :span='18'>
                                        <el-tag v-for="(item3) in item2.children" :key="item3.id" type="warning" closable @close="removeRightById(scope.row,item3.id)">{{item3.authName}}</el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <!-- 索引列 -->
                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作" prop="level" width="300px">
                    <!-- 作用域插槽自定义输出格式 -->
                    <template slot-scope="scope">
                        <!-- scope.row为这一行上的所有数据 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeRoleById(scope.row.id)">删除</el-button>
                        <el-button type="warning" icon="el-icon-setting" size="mini">分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 添加角色的对话框 -->
            <el-dialog
                title="添加角色"
                :visible.sync="addDialogVisible"
                width="50%">
                <!-- 内容主题区域 -->
                <span>
                    <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
                        <el-form-item label="角色名称" prop="roleName" label-width="100px">
                            <el-input v-model="addForm.roleName"></el-input>
                        </el-form-item>
                        <el-form-item label="角色描述" prop="roleDesc" label-width="100px">
                            <el-input v-model="addForm.roleDesc"></el-input>
                        </el-form-item>
                    </el-form>
                </span>
                <!-- 底部按钮区域 -->
                <span slot="footer" class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addRole">确 定</el-button>
                </span>
            </el-dialog>

            <!-- 编辑角色的对话框 -->
            <el-dialog
                title="编辑角色"
                :visible.sync="editDialogVisible"
                width="50%">
                <!-- 内容主题区域 -->
                <span>
                    <el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="70px">
                        <el-form-item label="角色名称" prop="roleName" label-width="100px">
                            <el-input v-model="editForm.roleName"></el-input>
                        </el-form-item>
                        <el-form-item label="角色描述" prop="roleDesc" label-width="100px">
                            <el-input v-model="editForm.roleDesc"></el-input>
                        </el-form-item>
                    </el-form>
                </span>
                <!-- 底部按钮区域 -->
                <span slot="footer" class="dialog-footer">
                    <el-button @click="editDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editRole">确 定</el-button>
                </span>
            </el-dialog>



        </el-card>
    </div>
</template>

<script>
export default {
    data(){
        return{
            rolesList : [],
            addForm:{
                roleName:'',
                roleDesc:''
            },
            addDialogVisible:false,
            // 添加角色的表单数据验证对象
            addFormRules:{
                roleName:[
                    { required: true, message: '请输入角色名', trigger: 'blur' },
                    { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
                ],
                roleDesc:[
                    { required: true, message: '请输入角色描述', trigger: 'blur' },
                    { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
                ]
            },
            editDialogVisible:false,
            editForm:{
                id:0,
                roleName:'',
                roleDesc:''
            },
        }
    },
    created(){
        this.getRolesList();
    },
    methods:{
        getRolesList:async function(){
            const {data:res} = await this.$http.get("roles");
            if(res.meta.status !== 200){
                return this.$message.error("获取数据失败！");
            }
            this.rolesList = res.data;
            console.log(this.rolesList);
        },
        addRole:function(){
            this.$refs.addFormRef.validate(async valid => {
                if(!valid) return;
                // 发起请求
                const {data:res} = await this.$http.post('roles',this.addForm);
                if(res.meta.status!==201) return this.$message.error("添加失败！");
                this.$message.success("添加成功！");
                // 隐藏添加用户的对话框
                this.addDialogVisible = false;
                // 刷新用户列表
                this.getRolesList();
            })
        },
        removeRoleById:async function(id){
            // 弹框询问用户是否删除
            const confirmResult = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(async () => {
                    const {data:res} = await this.$http.delete('roles/'+id);
                    if(res.meta.status !==200){
                        this.$message({
                            type: 'error',
                            message: '删除失败'
                        });
                    }
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getRolesList();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });       
                }
            );
        },
        showEditDialog:async function(id){
            const {data:res} = await this.$http.get('roles/'+id);
            if(res.meta.status!==200){
                return this.$message.error('查询角色数据失败');
            }
            this.editForm = res.data;
            this.editForm.id = id;
            this.editDialogVisible = true;
        },
        editRole:async function(){
            this.$refs.editFormRef.validate(async valid => {
                if(!valid) return;
                // 发起编辑角色的请求
                const {data:res} = await this.$http.put('roles/'+this.editForm.id,this.editForm);
                if(res.meta.status!==200) return this.$message.error("编辑失败！");
                this.$message.success("编辑成功！");
                // 隐藏添加角色的对话框
                this.editDialogVisible = false;
                // 刷新角色列表
                this.getRolesList();
            })
        },
        removeRightById:async function(role,rightId){
            // 弹框询问用户是否删除
            const roleId = role.id;
            const confirmResult = await this.$confirm('此操作将永久删除该角色的该权限, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(async () => {
                    const {data:res} = await this.$http.delete('roles/'+roleId+'/rights/'+rightId);
                    if(res.meta.status !==200){
                        this.$message({
                            type: 'error',
                            message: '删除失败'
                        });
                    }
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    // 刷新角色权限
                    role.children = res.data;

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });       
                }
            );
        },
    }
}
</script>

<style lang="scss" scoped>
.el-tag{
    margin: 7px;
}
.bdtop{
    border-top:1px solid #eee;
}
.bdbottom{
    border-bottom:1px solid #eee;
}
.vcenter{
    display:flex;
    align-items: center;
}
</style>